package com.majun.service.impl;

import com.majun.service.RocketMqService;
import com.majun.rmq.productor.RocketMqProducer;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RocketMqServiceImpl implements RocketMqService {

    @Resource
    private RocketMqProducer producer;

    private static final Logger LOGGER = LoggerFactory.getLogger(RocketMqServiceImpl.class);

    /**
     * @param tags    消息类型 如订单类型  消息类型 等 枚举在
     * @param message
     */
    @Override
    public void send(String tags, String message) {
        try {

            if (StringUtils.isBlank(tags) || StringUtils.isBlank(message)) {
                LOGGER.error("消息类型{} 或 消息内容为空{}", tags, message);
                throw new RuntimeException("消息类型 或者 消息内容为空 ");
            }

            Message msg = new Message();
            msg.setBody((message).getBytes(RemotingHelper.DEFAULT_CHARSET));
            msg.setTags(tags);
            SendResult sendResult = producer.send(msg);
            LOGGER.info("发送结果{}", sendResult);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("发送队列消息失败{}", e);
        }
    }
}
